home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / sbin / luksformat < prev    next >
Encoding:
Text File  |  2011-03-11  |  2.7 KB  |  109 lines

  1. #!/usr/bin/perl -w
  2.  
  3. # luksformat - wrapper around LUKS-capable cryptsetup and mkfs for easy
  4. # creation of an encrypted device.
  5. #
  6. # (C) 2005 Canonical Ltd.
  7. # Author: Martin Pitt <martin.pitt@ubuntu.com>
  8. # License: GNU General Public License, v2 or any later
  9. # (http://www.gnu.org/copyleft/gpl.html)
  10.  
  11. use Getopt::Long qw(:config pass_through);
  12.  
  13. sub usage() {
  14.     print "luksformat - Create and format an encrypted LUKS device
  15. Usage: luksformat [-t <file system>] <device> [ mkfs options ]\n";
  16.     exit 1;
  17. }
  18.  
  19. # default file system
  20. $fs = 'vfat';
  21. exit 1 unless GetOptions ('t|type=s' => \$fs);
  22.  
  23. GetOptions ('help', \$help);
  24. if (($#ARGV < 0) || ($help)) {
  25.     usage();
  26. }
  27.  
  28. if ($> != 0) {
  29.     print STDERR "This program needs to be started as root\n";
  30.     exit 1;
  31. }
  32.  
  33. $device = shift(@ARGV);
  34.  
  35. open(MOUNTS, "/proc/mounts");
  36. while (<MOUNTS>) {
  37.     die "Error: device mounted: $device\n" if (/\Q$device\E/)
  38. }
  39.  
  40. if (-x "/sbin/mkfs.$fs") {
  41.     $mkfs = "/sbin/mkfs.$fs";
  42. }
  43. elsif (-x "/usr/sbin/mkfs.$fs") {
  44.     $mkfs = "/usr/sbin/mkfs.$fs";
  45. }
  46. else {
  47.     print STDERR "Error: invalid file system: $fs\n";
  48.     exit 1;
  49. }
  50.  
  51. # generate temporary mapped device name which is not yet used
  52. $name = "";
  53. for ($i = 1; $i < 100; $i++) {
  54.     if (! -e "/dev/mapper/luksformat$i") {
  55.     $name = "luksformat$i";
  56.     last;
  57.     }
  58. }
  59.  
  60. $name or die "Error: could not generate temporary mapped device name";
  61.  
  62. # we do not need to be overly concerned with race conditions here, cryptsetup
  63. # will just fail if the name already exists now.
  64. print "Creating encrypted device on $device...\n";
  65. if ((system 'cryptsetup', 'luksFormat', '-s', '256', '--cipher', 'aes-cbc-essiv:sha256', $device)) {
  66.     die "Could not create LUKS device $device";
  67. }
  68.  
  69. print "Please enter your passphrase again to verify it\n";
  70. if ((system 'cryptsetup', 'luksOpen', $device, $name) != 0) {
  71.     print STDERR "The passphrases you entered were not identical\n";
  72.     exit 1;
  73. }
  74.  
  75. $result = system $mkfs, "/dev/mapper/$name", @ARGV;
  76. print "\n";
  77. system 'udevadm', 'settle', '--timeout=30';
  78. system 'cryptsetup', 'luksClose', $name;
  79.  
  80. die "Could not format device with file system $fs" if $result;
  81.  
  82. __END__
  83.  
  84. =head1 NAME
  85.  
  86. luksformat - Create and format an encrypted LUKS device 
  87.  
  88. =head1 SYNOPSIS
  89.  
  90. B<luksformat> [B<-t> I<fstype>] I<device> [ mkfs options ]
  91.  
  92. =head1 DESCRIPTION
  93.  
  94. B<luksformat> is a wrapper around B<cryptsetup> and B<mkfs> which provides an
  95. easy interface for creating an encrypted device that follows the LUKS standard
  96. and for putting a file system onto the encrypted device.
  97.  
  98. The default file system is B<vfat> since that is most commonly used on
  99. removable devices. However, you can specify any available file system with the
  100. B<-t> option.
  101.  
  102. =head1 SEE ALSO
  103.  
  104. L<cryptsetup(8)>, L<mkfs(8)>
  105.  
  106. =head1 AUTHOR
  107.  
  108. This program was written by Martin Pitt <martin.pitt@ubuntu.com>.
  109.